﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KerriganDataContract;
using System.Diagnostics;
using KerriganDataContract.Session;
using KerriganDataContract.Agency;
using KerriganDataContract.DrivingMode;
using System.ServiceModel;
using KerriganDataContract.Formation;
using KerriganDataContract.Intructor;

namespace KerriganService.DAC
{
    public class SessionCoursesDAC
    {
        #region Static Method

        public static SessionDC GetSession(int id)
        {
            SessionDC dc = new SessionDC();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    TSession entity = new TSession();
                    entity = db.TSessions.Where(t => t.IdSession == id).FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return dc;
        }

        /// <summary>
        /// Add and Update method for a Session Object
        /// </summary>
        /// <param name="dc"></param>
        public static void SetSession(SessionDC bo)
        {
            TSession entity = new TSession();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    if (bo.Id == 0)
                    {
                        entity = SessionCoursesDAC.CopyBoToEntity(bo);
                        db.AddToTSessions(entity);
                        db.SaveChanges();
                    }
                    else
                    {
                        entity = db.TSessions.Where(t => t.IdSession == bo.Id).FirstOrDefault();
                        entity = SessionCoursesDAC.CopyBoToEntity(bo);
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public static void DeleteSession(int id)
        {
            TSession entity = new TSession();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    entity = db.TSessions.Where(t => t.IdSession == id).FirstOrDefault();
                    db.DeleteObject(entity);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public static List<vwGetSession> GetListSessionsToDisplay ()
        {
            List<vwGetSession> list = new List<vwGetSession>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    list = db.vwGetSessions.ToList();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }


        public static List<SessionDC> GetSessionsByCustomer(int idCustomer)
        {
            List<SessionDC> list = new List<SessionDC>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {

                    list = CopyListEntitiesToListBos((from seesionApplicant in db.TSessionApplicants
                                                     where seesionApplicant.IdApplicantFK == idCustomer
                                                     select seesionApplicant.TSession).ToList());

                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }

        public static List<SessionDC> GetSessionsByCustomerWhitoutMarkTodayForInstructor( int idCustomer, int idInstructor)
        {
            List<SessionDC> list = new List<SessionDC>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {

                    list = CopyListEntitiesToListBos((from sessionApplicant in db.TSessionApplicants
                                                      where sessionApplicant.IdApplicantFK == idCustomer && sessionApplicant.TSession.Date.Day == DateTime.Now.Day && sessionApplicant.TSession.Date.Year == DateTime.Now.Year && sessionApplicant.TSession.Date.Month == DateTime.Now.Month && sessionApplicant.TCustomerEvaluation == null && sessionApplicant.TSession.IdInstructorFK == idInstructor
                                                      select sessionApplicant.TSession).ToList());

                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }




        public static List<SessionByCustomer_Result> GetAllSessionAgencyBycustomerAndOffer(int idCustomer, int idOffer, DateTime date)
        {
            List<SessionByCustomer_Result> list = new List<SessionByCustomer_Result>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {

                    list = db.SessionByCustomer(idCustomer, idOffer, date).ToList();

                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }

        public static List<SessionBycustomerLight_Result> GetAllSessionAgencyBycustomerAndOffer(int idCustomer)
        {
            List<SessionBycustomerLight_Result> list = new List<SessionBycustomerLight_Result>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {

                    list = db.SessionBycustomerLight(idCustomer).ToList();

                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }

        public static List<vwGetSessionApplicant> GetListSessionApplicantToDisplay()
        {
            List<vwGetSessionApplicant> list = new List<vwGetSessionApplicant>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    list = db.vwGetSessionApplicants.ToList();
                }   
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
            return list;
        }


        public static Boolean AddCustomerToSession(int idSession, int idCustomer)
        {
            Boolean result = false;
            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    TSessionApplicant entity = new TSessionApplicant();
                    TCustomer customer = db.TCustomers.SingleOrDefault(f => f.IdCustomer == idCustomer);
                    TSession session = db.TSessions.SingleOrDefault(f => f.IdSession == idSession);
                    entity.TSession = session;
                    entity.TCustomer = customer;
                    entity.IdFactureGenerated = null;
                    entity.IdCustomerEvaluationFK = null;
                    db.TSessionApplicants.AddObject(entity);
                    db.SaveChanges();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
            return result;
        }

        public static int DeleteCustomerToSession(int idSession, int idCustomer)
        {
            int result = 0;

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    TSessionApplicant entity = new TSessionApplicant();
                    entity = db.TSessionApplicants.FirstOrDefault(f => f.IdApplicantFK == idCustomer && f.IdSessionFK == idSession);
                    db.TSessionApplicants.DeleteObject(entity);
                    db.SaveChanges();
                    result = entity.IdSessionApplicant;
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
            return result;
        }



        #endregion

        #region Entity/Bo method

        public static SessionDC CopyEntityToBo(TSession entity)
        {
            SessionDC bo = new SessionDC();

            try
            {
                bo.Id = entity.IdSession;
                bo.Date = entity.Date;
                if(entity.TAgency != null){
                    bo.Agency = new AgencyDC();
                    bo.Agency = AgencyDAC.CopyEntityToBo(entity.TAgency);
                }

                bo.LevelSession = new LevelSessionDC();
                bo.LevelSession = LevelSessionDAC.CopyEntityToBo(entity.TLevelSession);
                bo.Formation = new FormationDC();
                bo.Formation = FormationDAC.CopyEntityToBo(entity.TFormation);
                bo.Instructor = new InstructorDC();
                bo.Instructor = InstructorDAC.CopyEntityToBo(entity.TInstructor);
                bo.EndDate = entity.EndDate;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return bo;
        }

        public static TSession CopyBoToEntity(SessionDC bo)
        {
            TSession entity = new TSession();

            try
            {
                entity.IdSession = bo.Id;
                entity.Date = bo.Date;
                entity.IdAgencyFK = bo.Agency.IdAgency;
                entity.IdLevelSessionFK = bo.LevelSession.IdLevelSessionDC;
                entity.IdInstructorFK = bo.Instructor.IdInstrutorDC;
                entity.IdFormationFK = bo.Formation.Id;
                entity.IdVehicleFK = bo.Vehicle.IdVehicle;
                entity.EndDate = bo.EndDate;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return entity;
        }

        public static List<SessionDC> CopyListEntitiesToListBos(List<TSession> entities)
        {
            List<SessionDC> bo = new List<SessionDC>();

            try
            {
                foreach (TSession entity in entities)
                {
                    bo.Add(CopyEntityToBo(entity));
                }
                return bo;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion
    }
}